package com.iims.product.service;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.constant.UserConstants;
import com.iims.product.domain.Product;
import com.iims.product.mapper.ProductMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/6/11 14:27
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Override

    public List<Product> selectProductList(Product product) {
        return baseMapper.selectProductList(product);
    }

    @Override
    public Product selectOne(Long productId) {
        return baseMapper.selectProductById(productId);
    }

    @Override
    public String checkProductNameUnique(Product product) {
        Long ProductId = Validator.isNull(product.getProductId()) ? -1L : product.getProductId();
        Product info = getOne(new LambdaQueryWrapper<Product>()
                .eq(Product::getProductName, product.getProductName()).last("limit 1"));
        if (Validator.isNotNull(info) && info.getProductId().longValue() != ProductId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertProduct(Product Product) {
        return baseMapper.insert(Product);
    }

    @Override
    public int updateProduct(Product Product) {
        return baseMapper.updateById(Product);
    }

    @Override
    public int deleteProductById(Long productId) {
        return baseMapper.deleteById(productId);
    }

    @Override
    public boolean checkProductUse(Long productId) {
        return baseMapper.checkProductUseCount(productId) > 0;
    }

}
